package com.zpark.wxshop.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.security.Key;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpark.wxshop.Utils.Result;

import com.zpark.wxshop.service.IProductService;
import com.zpark.wxshop.entity.Product;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PJone
 * @since 2023-09-04
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private IProductService productService;

    /**
    * 新增与修改
    */
    @PostMapping
    public Result save(@RequestBody Product product) {
        productService.saveOrUpdate(product);
        return Result.success();
    }

    /**
    * 根据id删除
    */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        productService.removeById(id);
        return Result.success();
    }

    /**
    * 批量删除
    */
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        productService.removeByIds(ids);
        return Result.success();
    }

    /**
    * 查询所有
    */
    @GetMapping
    public Result findAll(@RequestParam(value = "pageNum", defaultValue = "0") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws IOException {
        return Result.success(productService.FindProducts(null, null, null, null, null, null, pageNum, pageSize));
    }

    /**
    * 根据id查询单个
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) throws IOException {
        return Result.success(productService.FindById(id));
    }

    /**
     * 根据关键字模糊查询
     */
    @GetMapping("/key/{key}")
    public Result findBykey(@PathVariable String key) throws IOException {
//        return Result.success(productService.list(new QueryWrapper<Product>().like(true, "title ", key)));
        return Result.success(productService.FindByTitle(key));
    }

    /**
    * 分页查询
    */
    //    @GetMapping("/page")
    //    public Result findPage(@RequestParam Integer pageNum,
    //                                @RequestParam Integer pageSize) {
    //        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
    //        queryWrapper.orderByDesc("id");
    //        return Result.success(productService.page(new Page<>(pageNum, pageSize), queryWrapper));
    //    }

    /**
     * 根据店铺id查询商品
     */
    @GetMapping("/store/{store}")
    public Result findByStore(@PathVariable("store") Integer store){
        return Result.success(productService.list(new QueryWrapper<Product>().eq("store", store)));
    }

    /**
     * 在店铺内根据销售类型展示商品
     */
    @GetMapping("/store")
    public Result findBySale(@RequestParam("store") Integer store, @RequestParam("saleType") Integer saleType){
        return Result.success(productService.list(new QueryWrapper<Product>().eq("store", store).eq("sale_type", saleType)));
    }

    /**
     * 搜索店铺内的商品
     */
    @GetMapping("/product_in_shop")
    public Result findInShop(@RequestParam("key") String key, @RequestParam("store") Integer store){
        return Result.success(productService.list(new QueryWrapper<Product>().like(true, "title", key).eq("store", store)));
    }

    /**
     * 查询商品
     */
    @RequestMapping("/page")
    public Result FindProducts(@RequestParam(value = "title", required = false) String title,
                               @RequestParam(value = "shopName", required = false) String shopName,
                               @RequestParam(value = "startPrice", required = false) Integer StartPrice,
                               @RequestParam(value = "endPrice", required = false) Integer EndPrice,
                               @RequestParam(value = "startNum", required = false) Integer StartNum,
                               @RequestParam(value = "endNum", required = false) Integer EndNum,
                               @RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize) throws IOException {

        return Result.success(productService.FindProducts(title, shopName, StartPrice, EndPrice, StartNum, EndNum, pageNum, pageSize));

    }

}

